package com.sllp.C71S3TsllpShop.biz.impl;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.SessionAttribute;

import com.github.pagehelper.PageHelper;
import com.sllp.C71S3TsllpShop.bean.Address;
import com.sllp.C71S3TsllpShop.bean.AddressExample;
import com.sllp.C71S3TsllpShop.bean.Business;
import com.sllp.C71S3TsllpShop.bean.Result;
import com.sllp.C71S3TsllpShop.bean.User;
import com.sllp.C71S3TsllpShop.bean.UserExample;
import com.sllp.C71S3TsllpShop.bean.UserExample.Criteria;
import com.sllp.C71S3TsllpShop.biz.UserBiz;
import com.sllp.C71S3TsllpShop.dao.AddressMapper;
import com.sllp.C71S3TsllpShop.dao.UserMapper;
import com.sllp.C71S3TsllpShop.util.SendEmail;


@Service
public class UserBizImpl implements UserBiz {

	@Resource
	SendEmail se ;
	
	@Resource
	private UserMapper um;
	
	@Resource
	private AddressMapper am;
	
	public List<User> userMsg(User user){
		UserExample ue=new UserExample();
		Criteria c=ue.createCriteria();
		c.andIdEqualTo(user.getId());
		List<User> list=um.selectByExample(ue);
		return list;
	}
	@Override
	public Result userLogin(User user) {
		/*
		 * 登录需要搜索数据库中是否有此用户名和密码
		 * 若有则需要查看此用户的登录状态是否为在线状态如果为在线状态则不让登录
		 * 否则修改此人登录状态为已登陆
		 * 
		 * 优化点：是否直接搜索未登录的用户会好一些
		 */
		String md5Password = DigestUtils.md5DigestAsHex(user.getPwd().getBytes());
		UserExample example = new UserExample();
		example.createCriteria()
			.andLoginNameEqualTo(user.getLoginName())
			.andPwdEqualTo(md5Password);
		List<User> users = um.selectByExample(example);
		if(users.size()>1) {
			// 用户存在重复，要求管理员处理，此处可发送邮件给数据库管理员处理这个情况
			return new Result(0, "用户存在重复现象",users);
		}else if(users.size()==0){
			return new Result(0,"用户名或密码错误",user);
		}
		
		User loginUser = users.get(0);
		loginUser.setLoginState(1);
		int updateResult = um.updateByPrimaryKey(loginUser);
		if(updateResult<0) {
			return new Result(0,"登陆失败",loginUser);
		}
		return new Result(1,"登陆成功",loginUser);
	}

	@Override
	public Result userRegist(User user) {
		UserExample userExample = new UserExample();
		userExample.createCriteria().andTelEqualTo(user.getTel());
		List<User> userList = um.selectByExample(userExample);
		if(userList.size()>0) {
			return new Result(0, "手机号已被使用");
		}
		int result = um.insert(user);
		if(result>0) {
			return new Result(1, "注册成功");
		}
		return new Result(0, "注册失败");
	}
	
	

	@Override
	public User selectByPrimaryKey(int id) {
		User list = um.selectByPrimaryKey(id);
		return list;
	}

	@Override
	public Result userLogout(User user) {
		UserExample userExample = new UserExample();
		userExample.createCriteria().andLoginNameEqualTo(user.getLoginName());
		int flag = um.updateByExampleSelective(user, userExample);
		if(flag>0) {
			return new Result(1, "登出成功");
		}
		return new Result(0, "登出失败");
	}

	@Override
	public Result findUserEmail(String loginName) {
		UserExample userExample = new UserExample();
		userExample.createCriteria().andLoginNameEqualTo(loginName);
		List<User> users = um.selectByExample(userExample);
		if(users.size()>0) {
			return new Result(1, "查询成功", users.get(0));
		}
		return new Result(0, "查无此人");
	}

	@Override
	public Result updateUserPwd(User user) {
		UserExample userExample = new UserExample();
		userExample.createCriteria().andLoginNameEqualTo(user.getLoginName());
		List<User> users = um.selectByExample(userExample);
		if(users.get(0).getPwd().equals(user.getPwd())) {
			return new Result(0, "新密码不能与旧密码相同");
		}
		int flag = um.updateByExampleSelective(user, userExample);
		if(flag>0) {
			return new Result(1, "重置成功");
		}
		return new Result(0, "重置密码失败");
	}
	
	
	public Result ToSendEmail21(String subrject ,User user,String from ,String content,String code) {
		Result result= se.sendEmail(subrject, from, user.getEmail(), content, code);
		return result;
	}

	//修改时  邮箱验证
	public Result ToSendEmail22(String subrject ,User user,String from ,String content,String code) {
		Result result= se.sendEmail(subrject, from, user.getEmail(), content, code);
		return result;
	}

	//修改时  邮箱验证
	public Result ToSendEmail23(String subrject ,User user,String from ,String content,String code) {
		Result result= se.sendEmail(subrject, from, user.getEmail(), content, code);
		return result;
	}

	/**
	 * 修改手机号
	 * @param tel2
	 * @return
	 */
	public Result Modifytel(String tel2,User user) {
			
			
			int ok = 0 ;
			User u =new User();
			Address a =new Address();
			UserExample ue =new UserExample();
			AddressExample ae =new AddressExample();
			
			u.setTel(tel2);
			a.setTel(tel2);
			ue.createCriteria().andIdEqualTo(user.getId());
			ae.createCriteria().andUserEqualTo(user.getId());
				
			ok=um.updateByExampleSelective(u, ue);
			if(ok<0) {
				return new Result(2, "重置失败");
			}
			ok=am.updateByExampleSelective(a, ae);
			if(ok>0) {
				return new Result(1, "重置成功");
			}else {
				return new Result(2, "重置失败");
			}
			
			
		}
	
		//修改eamil
		public Result Modifyemail(String email,User user) {
			int ok = 0 ;
			User u =new User();
			UserExample ue =new UserExample();
			
			u.setEmail(email);
			ue.createCriteria().andIdEqualTo(user.getId());
			ok=um.updateByExampleSelective(u, ue);
			if(ok>0) {
				return new Result(1, "重置成功");
			}else {
				return new Result(2, "重置失败");
			}
		}
		

		//修改密码
			public Result Modifypwd(String pwd,User user) {
				int ok = 0 ;
				User u =new User();
				UserExample ue =new UserExample();
				
				u.setPwd(pwd);
				ue.createCriteria().andIdEqualTo(user.getId());
				ok=um.updateByExampleSelective(u, ue);
				
				if(ok>0) {
					return new Result(1, "重置成功");
				}else {
					return new Result(2, "重置失败");
				}
			}
/*	@Resource
	private UserMapper um;

	@Override
	public List<User> selectByProduct(int id) {
		List<User> list = um.selectByProduct(id);
		return list;
	}
*/
	/**
	 * 修改用户的默认地址
	 */
	public int updateAddress(int id,User loginUser){
		User user=new User();
		user.setAddr(id);
		UserExample ue=new UserExample();
		Criteria c=ue.createCriteria();
		c.andIdEqualTo(loginUser.getId());
		int i=um.updateByExampleSelective(user, ue);
		return i;
	}
	
	@Override
	public List<User> findUserByCondition(User user, Integer page, Integer pageSize){
        UserExample ue=new UserExample();
        Criteria c=ue.createCriteria();
		if(user!=null) {
			if(user.getId()!=null) {
				c.andIdEqualTo(user.getId());
			}
			if(user.getTrueName()!=null) {
				c.andTrueNameLike("%"+user.getTrueName()+"%");
			}
			if(user.getState()!=null) {
				c.andStateEqualTo(user.getState());
			}
		}
		List<User> userList = null;
		if(page!=null) {
			PageHelper.startPage(page, pageSize);
			userList = um.selectByExampleUser(ue);
		}else {
			userList = um.selectByExampleUser(ue);
		}
		return userList;
	}
	
	@Override
	public Result updateUserState(User user) {
		if(user.getState()==2) {
			user.setState(3);
		}else if(user.getState()==3) {
			user.setState(1);
		}else if(user.getState()==1) {
			user.setState(3);
		}
		int code = um.updateByPrimaryKeySelective(user);
		if(code>0) {
			return new Result(1, "修改成功");
		}
		return new Result(0, "修改失败");
	}
	
	public User selectUser(){
		User user=um.selectByPrimaryKey(1);
		return user;
	}
	
	public Result updateUserImage(@SessionAttribute("loginUser") User user){
		UserExample ue=new UserExample();
		Criteria c=ue.createCriteria();
		c.andIdEqualTo(user.getId());
		int result=um.updateByExample(user, ue);
		if(result>0) {
			return new Result(1, "成功");
		}
		return new Result(0, "失败");
	}
}
